# Maintainer: Haroogan <Haroogan@gmail.com>
# Maintainer: Oscar Fuentes <ofv@wanadoo.es>

_enable_jit=$([[ "${MINGW_PREFIX}" =~ /clang.* ]] || echo yes)
_realname=emacs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=28.2
pkgrel=2
pkgdesc="The extensible, customizable, self-documenting, real-time display editor (mingw-w64)"
url="https://www.gnu.org/software/${_realname}/"
license=('spdx:GPL-3.0')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-universal-ctags-git"
	 $([[ "$_enable_jit" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-libgccjit")
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
	 "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-jansson"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
optdepends=("${MINGW_PACKAGE_PREFIX}-giflib"
            "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
            "${MINGW_PACKAGE_PREFIX}-libpng"
            "${MINGW_PACKAGE_PREFIX}-librsvg"
            "${MINGW_PACKAGE_PREFIX}-libtiff"
            "${MINGW_PACKAGE_PREFIX}-libxml2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "texinfo"
             "patch"
	     "git"
             "${optdepends[@]}")
# Don't zip info files because the built-in info reader uses gzip to
# decompress them. gzip is not available as a mingw binary.
options=('strip' '!zipman')
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig}
        "001-ucrt.patch"
        "002-clang-fixes.patch")
# source=("https://alpha.gnu.org/gnu/${_realname}/pretest/${_realname}-${pkgver}.tar.xz"{,.sig})
sha256sums=('ee21182233ef3232dc97b486af2d86e14042dbb65bbc535df562c3a858232488'
            'SKIP'
            'e1347064ec30094e21679764f784fa7557738946485359041473e6e9d7f3c3dc'
            '0543a3082262077edb89f2ee1af2531025888be7d15c8255d6c67fb732711781')
validpgpkeys=('28D3BED851FDF3AB57FEF93C233587A47C207910'
	      '17E90D521672C04631B1183EE78DAE0F3115E06B'
	      'E6C9029C363AD41D787A8EBB91C1262F01EB8D39'
	      'CEA1DE21AB108493CC9C65742E82323B8F4353EE')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/001-ucrt.patch"
  patch -Np1 -i "${srcdir}/002-clang-fixes.patch"

  ./autogen.sh
}

build() {
  [[ -d "build-${MSYSTEM}" ]] && rm -rf "build-${MSYSTEM}"
  mkdir -p "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  _extra_cfg=""
  if [[ "$_enable_jit" == "yes" ]] ; then
      _extra_cfg="$_extra_cfg --with-native-compilation"
  fi
  ../${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --host="${MINGW_CHOST}" \
    --build="${MINGW_CHOST}" \
    --with-modules \
    --without-dbus \
    --without-compress-install \
    $_extra_cfg

  # --without-compress-install is needed because we don't have gzip in
  # the mingw binaries and it is also required by native compilation.

  make
}

package() {
  cd "build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  rm -f "${pkgdir}${MINGW_PREFIX}/bin/ctags.exe"
  rm -f "${pkgdir}${MINGW_PREFIX}/share/man/man1/ctags.1.gz"

  local dir="${pkgdir}${MINGW_PREFIX}/share/${_realname}"
        dir="${dir}/$(ls -1 ${dir} | grep -E '([0-9]+\.[0-9]+)(\.[0-9]+)?')/src"

  mkdir -p "${dir}"
  cd "${srcdir}/${_realname}-${pkgver}/src"
  cp *.c *.h *.m "${dir}"
}

# TODO:
# Patch `shell-file-name' default in the C source code similarly to
# `source-directory'.
